//
//  YXClassRoomViewController.m
//  CreditCard
//
//  Created by 杨旭 on 2017/12/23.
//  Copyright © 2017年 杨旭. All rights reserved.
//

#import "YXClassRoomViewController.h"
#import "SDCycleScrollView.h"
#import "YXCustomSectionView.h"
#import "YXClassRoomCell.h"
#import "YXHomeViewModel.h"
#import "YXClassRoomModel.h"
#import "YXClassRoomDetailsViewController.h"
#define kLeftTableView 5000
#define kRightTableView 5001
@interface YXClassRoomViewController ()<UITableViewDelegate,UITableViewDataSource, SDCycleScrollViewDelegate>
@property (nonatomic ,strong) SDCycleScrollView * cycleScrollView;
@property (nonatomic ,strong) UITableView *leftTableView;
@property (nonatomic ,strong) UITableView *rightTableView;
@property (nonatomic ,strong) UIView *headerView;
@property (nonatomic ,strong) NSMutableArray *dataArr;
@end

@implementation YXClassRoomViewController

- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

- (UIView *)headerView {
    if (!_headerView) {
        _headerView = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, kScreenWidth, 230))];
        _cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, kScreenWidth, 150) delegate:self placeholderImage:nil];
        [_headerView addSubview:self.cycleScrollView];
        
        YXCustomSectionView *sectionView = [[YXCustomSectionView alloc] initWithFrame:(CGRectMake(0, 150, kScreenWidth, 80)) withTieleArr:@[@"基础必修课",@"中级必修课"] withImageArr:nil];
        sectionView.backgroundColor = [UIColor whiteColor];
        [_headerView addSubview:sectionView];
        [sectionView setSelectBtnClick:^(NSInteger index) {
            if (index == 0) {
                [self requestDataListType:@"1"];
                [self.rightTableView removeFromSuperview];
                [self createLeftTableViewUI];
                
            }else {
                [self requestDataListType:@"2"];
                [self.leftTableView removeFromSuperview];
                [self createRightTableViewUI];
                
            }
        }];
        
        
        
    }
    return  _headerView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self createLeftBarButtonItemWithImage:@"导航栏_002"];
    [self requestDataListType:@"1"];
    [self createLeftTableViewUI];
    [YXHomeViewModel getQuerySelectBannerListCategory:@"2" Type:nil ModelList:^(NSArray *listArr,YXBannerModel *model) {
        _cycleScrollView.imageURLStringsGroup = listArr;
        
    } Failure:^(NSError *error) {
        [self showAlertWithTitle:@"提示" Messge:@"维护中，请稍等！"];
    }];
}
- (void)requestDataListType:(NSString *)type {
    
//    http://121.196.210.134:8082/mpos/wx/getClassroomList.do
    NSString *urlStr = @"getClassroomList.do";
    NSDictionary *parameter = @{@"type":type};
    [BX_NetWorkManager requestForPOSTWithUrl:urlStr parameter:parameter success:^(id responseObject) {
        NSLog(@"%@",responseObject);
        [self.dataArr removeAllObjects];
        if (REQUESTDATASUCCESS) {
            self.dataArr = [YXClassRoomModel mj_objectArrayWithKeyValuesArray:responseObject[@"body"]];
        }
        [self.leftTableView reloadData];
        [self.rightTableView reloadData];
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        [self showAlertWithTitle:@"提示" Messge:@"维护中，请稍等！"];
    }];
    
}


- (void)createLeftTableViewUI {
    self.leftTableView =[[UITableView alloc] initWithFrame:(CGRectMake(0, 0, kScreenWidth , kScreenHeight))];
    self.leftTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.leftTableView.backgroundColor = color_backColor;
    self.leftTableView.delegate = self;
    self.leftTableView.dataSource = self;
    self.leftTableView.tag = kLeftTableView;
    [self.view addSubview:self.leftTableView];
    _leftTableView.tableHeaderView = self.headerView;
    
}
- (void)createRightTableViewUI {
    
    self.rightTableView = [[UITableView alloc] initWithFrame:(CGRectMake(0, 0, kScreenWidth, kScreenHeight))];
    self.rightTableView.backgroundColor = color_backColor;
    self.rightTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.rightTableView.delegate = self;
    self.rightTableView.dataSource = self;
    self.rightTableView.tag = kRightTableView;
    [self.view addSubview:self.rightTableView];
    _rightTableView.tableHeaderView = self.headerView;

}

#pragma mark - UITableView Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 110;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString * cellId = @"YXClassRoomCell";
    YXClassRoomCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell==nil) {
        cell=[[[NSBundle mainBundle]loadNibNamed:cellId owner:self options:nil] lastObject];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    }
    cell.model = self.dataArr[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    YXClassRoomDetailsViewController *detailsVC = [[YXClassRoomDetailsViewController alloc] init];
    YXClassRoomModel *model = self.dataArr[indexPath.row];
    detailsVC.ID = model.ID;
    [self.navigationController pushViewController:detailsVC animated:YES];
}

@end
